<?php
class Apartment extends AppModel {

	var $name = 'Apartment';
	var $primaryKey = 'apartment_id';

	var $validate = array(
		'apartment_id' => VALID_NUMBER,
		'apartmentNumber' => VALID_NOT_EMPTY,
		'language_id' => VALID_NUMBER,
		'gender' => VALID_NOT_EMPTY
	);

	var $belongsTo= array(
			'Language' =>
				array('className' => 'Language',
						'foreignKey' => 'language_id'
				),
			'Semester' =>
				array('className' => 'Semester',
						'foreignKey' => 'semester_id'
				)
	);

	function getApartmentList() {
		return $this->generateList(null, null, null, "{n}.Apartment.apartment_id", "{n}.Apartment.apartmentNumber");
	}

	function getNumberAndSemesterList() {
					$apartments = $this->query("SELECT a.apartment_id, a.apartmentNumber, s.year, t.description FROM
									apartments a,
									semesters s,
									terms t
									WHERE 
									a.semester_id = s.semester_id AND
									s.term_id = t.term_id");
					$apartmentList = array();
					foreach ($apartments as $apartment) {
						$apartmentId = $apartment['a']['apartment_id'];
						$apartmentNumber = $apartment['a']['apartmentNumber'];
						$year = $apartment['s']['year'];
						$term = $apartment['t']['description'];

						$apartmentList[$apartmentId] = "$apartmentNumber ($term $year)";
					}

					return $apartmentList;
	}
}
?>
